/*
*  CELabelBMFont.h 
*  LID Library  Cocos2d-x extentions
*
*  Created by Alexander Trutchenko on 10/01/12.
*  Copyright 2011 League of Independent Developers. All rights reserved.
*
*/


#ifndef __CE_LabelBMFont_h__
#define __CE_LabelBMFont_h__

#include "cocos2dx_extentions/labels/CELabelProtocol.h"

#include "lid_lib/src/types/LLTypes.h"

#include "CCLabelBMFont.h"

#include <map>


namespace cocosext{

////////////////////////////////////////////////////////
//
/** CEBMFontConfiguration
* It is recreation of CCBMFontConfiguration because of change _CCKerningHashElement to _CEKerningHashElement
*/
////////////////////////////////////////////////////////

struct _CEKerningHashElement;


typedef std::map<unsigned int, cocos2d::ccBMFontDef> CECharacters;

class CEBMFontConfiguration : public cocos2d::CCObject
{
	// XXX: Creating a public interface so that the bitmapFontArray[] is accesible
public://@public
	//! The characters building up the font
    CECharacters	m_characters;
	//! FNTConfig: Common Height
	unsigned int m_uCommonHeight;
	//! Padding
	cocos2d::ccBMFontPadding	m_tPadding;
	//! atlas name
	std::string m_sAtlasName;
	//! values for kerning
	struct _CEKerningHashElement	*m_pKerningDictionary;
	// Description
	std::string m_sDescription;

protected:
	CEBMFontConfiguration()
		: m_uCommonHeight(0)
        , m_pKerningDictionary(NULL)
	{}

public:
	virtual ~CEBMFontConfiguration();
	
	/** allocates a CCBMFontConfiguration with a FNT file */
	static CEBMFontConfiguration * configurationWithFNTFile(const char *FNTfile);
	/** initializes a BitmapFontConfiguration with a FNT file */
	bool initWithFNTfile(const char *FNTfile);
	const char * description();

	const std::string& atlasName() const { return m_sAtlasName; }

private:
	void parseConfigFile(const char *controlFile);
	void parseCharacterDefinition(std::string line, cocos2d::ccBMFontDef *characterDefinition);
	void parseInfoArguments(std::string line);
	void parseCommonArguments(std::string line);
	void parseImageFileName(std::string line, const char *fntFile);
	void parseKerningCapacity(std::string line);
	void parseKerningEntry(std::string line);
	void purgeKerningDictionary();
};

////////////////////////////////////////////////////////
//
/** CELabelBMFont
* It is recreation of CCLabelBMFont for wide char strings 
*/
////////////////////////////////////////////////////////

class CELabelBMFont : public cocos2d::CCSpriteBatchNode, public CELabelProtocol, public cocos2d::CCRGBAProtocol
{
	/** conforms to CCRGBAProtocol protocol */
	CC_PROPERTY(GLubyte, m_cOpacity, Opacity)
	/** conforms to CCRGBAProtocol protocol */
	CC_PROPERTY_PASS_BY_REF(cocos2d::ccColor3B, m_tColor, Color)
	/** conforms to CCRGBAProtocol protocol */
	CC_PROPERTY(bool, m_bIsOpacityModifyRGB, IsOpacityModifyRGB)
protected:
	// string to render
	std::wstring m_sString;
	CEBMFontConfiguration *m_pConfiguration;

protected:
	CELabelBMFont()
		: m_cOpacity(0)           
		, m_bIsOpacityModifyRGB(false)
        , m_pConfiguration(NULL)
	{}

public:
	virtual ~CELabelBMFont();
	/** Purges the cached data.
	Removes from memory the cached configurations and the atlas name dictionary.
	@since v0.99.3
	*/
	static void purgeCachedData();
	/** creates a bitmap font altas with an initial string and the FNT file */
	static CELabelBMFont * labelWithString(const wchar_t *str, const char *fntFile);

	/** init a bitmap font altas with an initial string and the FNT file */
	bool initWithString(const wchar_t *str, const char *fntFile);
	/** updates the font chars based on the string to render */
	void createFontChars();
	// super method
	virtual void setString(const wchar_t *label);
	virtual const wchar_t* getString(void) const;
    virtual void setCString(const wchar_t *label);
	virtual void setAnchorPoint(const cocos2d::CCPoint& var);
    virtual cocos2d::CCRGBAProtocol* convertToRGBAProtocol() { return (cocos2d::CCRGBAProtocol*)this; }
	virtual cocos2d::CCLabelProtocol* convertToLabelProtocol() { return (cocos2d::CCLabelProtocol*)this; }
#if CC_LABELBMFONT_DEBUG_DRAW
	virtual void draw();
#endif // CC_LABELBMFONT_DEBUG_DRAW
private:
	char * atlasNameFromFntFile(const char *fntFile);
	int kerningAmountForFirst(unsigned int first, unsigned int second);

};

////////////////////////////////////////////////////////
//
/** Help functions for working with CELabelBMFont labels
*/
////////////////////////////////////////////////////////

/** Free function that parses a FNT file a place it on the cache
*/
extern CEBMFontConfiguration * ceFNTConfigLoadFile( const char *file );
/** Purges the FNT config cache
*/
extern void ceFNTConfigRemoveCache( void );
extern void removeUnusedFNTConfigs();

}// namespace cocosext

#endif //__CE_LabelBMFont_h__
